Drawing maps


Here we will draw maps given a set of coordinates, using free services, and in several different styles. We will also use geolocation to find coordinates given the name of a place.

Import libraries

Install them if necessary:

Now import:

Plot a zoomable USGS-based map with Plotly

Open Street Maps

Using the stamen-terrain variation

Export the plotly map to an HTML file

Note: exporting a Plotly map to PNG is a lot more difficult, and I do not cover it here (yet).

Static maps using Matplotlib & Cartopy

Add layers to a basic map

Different layers can be added with add_feature. Below, we add LAND and OCEAN.


Not super pretty, but it works.

A natural-surface style

This looks very nice for continental distances, but if we try to use it for a dataset of nearby points like the one for NYC that we made above, it pixelates so horrendously it becomes useless:

With this kind of distance, this map style is not useful. We could try to zoom out by changing the x and y limits, but then we lose the points we were trying to observe:

P.S.: Pandas provides much more efficient ways of plotting the points than the for loop used above, but this is not a tutorial about Pandas.

Finding locations by name with a geocoder

Just like map providers, there are also many geolocators. You're no doubt familiar with Google Maps. We could access their geolocation system through Python but, unlike the on the Google Maps website, the programmatic one isn't free. There are options, however.

We imported geopy above, which has a geocoder called Nominatim that can find locations by name.

Let's try it with a few trivial and less-than-trivial names:


Mostly great in my case, except for The Big Easy and The Great Sphinx, which were complete failures (it may be different when you run it). We should probably go back and fix those (e.g., add "of Giza" to the sphinx). Note also that the Springfield in Illinois was favoured by the geolocator, which perhaps makes sense as that one is a State capital.

In fact, we have the coordinates:

This would be a much better use case for our nice National-Geographic-looking map:

Well, the positioning and sizing of the texts will need some work, but this is not a mabplotlib tutorial, so this will do for now. Also remember that we need to fix the Sphinx and New Orleans, both of which are utterly wrong.